﻿using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using DTO;

namespace DAL
{
    public class WebtagDAL
    {
        /// <summary>
        /// This method deletes a tag from database with given tagID
        /// </summary>
        /// <param name="tagID">The ID of the tag</param>
        /// <returns>True if delete successfully, false if not</returns>
        public static bool DeleteTag(int tagID)
        {
            // get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            // set the stored procedure name
            comm.CommandText = "DeleteTag";
            // create a new parameter
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@WebtagID";
            param.Value = tagID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            // execute the stored procedure and save the results in a DataTable
            return GenericDataAccess.ExecuteNonQuery(comm) > 0;
        }

        /// <summary>
        /// This method retrieves the root tag of the current webpage.
        /// </summary>
        /// <param name="webpageID">Current webpage</param>
        /// <returns>Root tag of current webpage</returns>
        public static Webtag GetRootTag(int webpageID)
        {
            // get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            // set the stored procedure name
            comm.CommandText = "GetRootTag";

            // create a new parameter
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@WebpageID";
            param.Value = webpageID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            // execute the stored procedure and return the results
            DataTable dt = GenericDataAccess.ExecuteSelectCommand(comm);
            DataRow dr = dt.Rows[0];
            Webtag curTag = new Webtag();
            curTag.ID = (int)dr["ID"];
            curTag.Name = (string)dr["Name"];
            curTag.Text = (string)dr["Text"];
            curTag.XPath = (string)dr["xPath"];
            curTag.WebpageID = (int)dr["WebpageID"];
            return curTag;
        }

        /// <summary>
        /// This method retrieves all children tags of current webtag in this webpage
        /// </summary>
        /// <param name="curWebtagID">Current webtag ID</param>
        /// <param name="webpageID">Current webpage ID</param>
        /// <returns>List of all children tags of the given webtag</returns>
        public static List<Webtag> GetChildTags(int curWebtagID, int webpageID)
        {
            // get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            // set the stored procedure name
            comm.CommandText = "GetChildTags";

            // create a new parameter
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@WebpageID";
            param.Value = webpageID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@ParentID";
            param.Value = curWebtagID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            List<Webtag> tagsList = new List<Webtag>();
            // execute the stored procedure and return the results
            DataTable dt = GenericDataAccess.ExecuteSelectCommand(comm);
            foreach (DataRow dr in dt.Rows)
            {
                Webtag curTag = new Webtag();
                curTag.ID = (int)dr["ID"];
                curTag.Name = (string)dr["Name"];
                curTag.Text = (string)dr["Text"];
                curTag.XPath = (string)dr["xPath"];
                curTag.WebpageID = (int)dr["WebpageID"];
                tagsList.Add(curTag);
            }
            return tagsList;
        }

        /// <summary>
        /// This method retrieves all tags of a webpage with given webpage ID.
        /// </summary>
        /// <param name="URL">The ID of the webpage needs to get tags</param>
        /// <returns>List of all tags of a webpage.</returns>
        public static List<Webtag> GetTags(int WebpageID)
        {
            // get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            // set the stored procedure name
            comm.CommandText = "GetTags";

            // create a new parameter
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@WebpageID";
            param.Value = WebpageID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            List<Webtag> tagsList = new List<Webtag>();
            // execute the stored procedure and return the results
            DataTable dt = GenericDataAccess.ExecuteSelectCommand(comm);
            foreach (DataRow dr in dt.Rows)
            {
                Webtag curTag = new Webtag();
                curTag.ID = (int)dr["ID"];
                curTag.Name = (string)dr["Name"];
                curTag.Text = (string)dr["Text"];
                curTag.XPath = (string)dr["xPath"];
                
                tagsList.Add(curTag);
            }
            return tagsList;
        }

        /// <summary>
        /// This method gets a webtag and then inserts this webtag with into database.
        /// </summary>
        /// <param name="curWebtag">The webpage needs to be inserted into database.</param>
        /// <param name="WebpageID">The webpage ID of the web page that this tag belongs to</param>
        /// <returns>ID of the inserted tag, -1 if not insert successfully</returns>
        public static int InsertTag(Webtag curWebtag, int WebpageID)
        {
            // get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            // set the stored procedure name
            comm.CommandText = "InsertTag";

            // create a new parameter
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@Name";
            param.Value = curWebtag.Name;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            // parentID = -1 --> root node
            param = comm.CreateParameter();
            param.ParameterName = "@ParentID";
            if (curWebtag.Parent == null)
                param.Value = -1;
            else
                param.Value = curWebtag.Parent.ID;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@Text";
            if (curWebtag.Text == null)
                param.Value = string.Empty;
            else
                param.Value = curWebtag.Text;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@xPath";
            param.Value = curWebtag.XPath;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            // create a new parameter
            param = comm.CreateParameter();
            param.ParameterName = "@WebpageID";
            param.Value = WebpageID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@ID";
            param.DbType = DbType.Int32;
            param.Direction = ParameterDirection.Output;
            comm.Parameters.Add(param);

            // execute the stored procedure and save the results in a DataTable
            GenericDataAccess.ExecuteNonQuery(comm);
            return (int)param.Value;
        }

        /// <summary>
        /// This method retrieves the tag id with given tag's xpath.
        /// </summary>
        /// <param name="xPath">Xpath of the current tag</param>
        /// <returns>The ID of this tag</returns>
        public static int GetTagID(string xPath)
        {
            // get a configured DbCommand object
            DbCommand comm = GenericDataAccess.CreateCommand();
            // set the stored procedure name
            comm.CommandText = "GetTagID";

            // create a new parameter
            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@xPath";
            param.Value = xPath;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            // execute the stored procedure and return the results
            DataTable dt = GenericDataAccess.ExecuteSelectCommand(comm);
            DataRow dr = dt.Rows[0];
            return (int)dr["ID"];
        }
    }
}
